﻿Imports System.IO

Public Class frmCapNhatKho

    Dim dt As DataTable
    Public IDHangHoa As Integer
    Public TenHang As String
    Public Gia As Integer
    Public SoLuong As Integer
    Public DonVi As Integer
    Public NhaCungCap As Integer
    Public XuatXu As Integer
    Public LoaiHang As Integer
    Public ViTri As Integer
    Public LinkPic As String

    Private Sub frmCapNhatKho_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        picHinhAnh.ImageLocation = LinkPic
        lbIDHangHoa.Text = IDHangHoa.ToString()
        txtTenHang.Text = TenHang
        txtGia.Text = Gia.ToString()
        txtSoLuong.Text = SoLuong.ToString()
        Me.Text = "Cập nhật hàng hóa trong kho"
        LoadCombobox()
    End Sub

    Sub LoadCombobox()
        dt = TapHoaDAO.LayDonViKho(DonVi)
        cbbDonVi.DataSource = dt
        cbbDonVi.DisplayMember = "dvt_Loai"
        cbbDonVi.ValueMember = "dvt_ID"

        dt = TapHoaDAO.LayNhaCungCapKho(NhaCungCap)
        cbbNhaCungCap.DataSource = dt
        cbbNhaCungCap.DisplayMember = "ncc_Ten"
        cbbNhaCungCap.ValueMember = "ncc_ID"

        dt = TapHoaDAO.LayXuatXuKho(XuatXu)
        cbbXuatXu.DataSource = dt
        cbbXuatXu.DisplayMember = "xx_NoiXuatXu"
        cbbXuatXu.ValueMember = "xx_ID"

        dt = TapHoaDAO.LayLoaiHangKho(LoaiHang)
        cbbLoaiHang.DataSource = dt
        cbbLoaiHang.DisplayMember = "lh_Loai"
        cbbLoaiHang.ValueMember = "lh_ID"

        dt = TapHoaDAO.LayViTriKho(ViTri)
        cbbViTri.DataSource = dt
        cbbViTri.DisplayMember = "vt_NoiVT"
        cbbViTri.ValueMember = "vt_ID"
    End Sub

    Private Sub btnThem_Click(sender As System.Object, e As System.EventArgs) Handles btnCapNhat.Click
        If txtGia.Text = "" Or txtSoLuong.Text = "" Or txtTenHang.Text = "" Then
            MessageBox.Show("bạn cần nhập đầy đủ thông tin")
            Return
        End If
        Dim _taphoa As New TapHoa()
        _taphoa.m_intIDHangHoa = Integer.Parse(IDHangHoa)
        _taphoa.m_strTenHangHoa = txtTenHang.Text
        _taphoa.m_intGia = Integer.Parse(txtGia.Text)
        _taphoa.m_intIDDonVi = Integer.Parse(cbbDonVi.SelectedValue.ToString())
        _taphoa.m_intSoLuong = Integer.Parse(txtSoLuong.Text)
        _taphoa.m_intIDNhaCungCap = Integer.Parse(cbbNhaCungCap.SelectedValue.ToString())
        _taphoa.m_intIDXuatXu = Integer.Parse(cbbXuatXu.SelectedValue.ToString())
        _taphoa.m_intIDLoaiHang = Integer.Parse(cbbLoaiHang.SelectedValue.ToString())
        _taphoa.m_intIDViTri = Integer.Parse(cbbViTri.SelectedValue.ToString())
        Dim DuongDan As String
        If (picHinhAnh.ImageLocation <> "../../HinhAnh/noimage.jpg") Then
            Dim directoryPath As String = "../../HinhAnh"
            Directory.CreateDirectory(directoryPath)
            Dim sourcefile As String = picHinhAnh.ImageLocation.ToString()
            Dim ChuyenDoi As String = directoryPath + "/" + Path.GetFileName(sourcefile)
            File.Copy(sourcefile, ChuyenDoi)
            DuongDan = ChuyenDoi
        Else
            DuongDan = "../../HinhAnh/noimage.jpg"
        End If
        _taphoa.m_strHinhAnh = DuongDan

        TapHoaDAO.CapNhatHangHoa(_taphoa)
        MessageBox.Show("Cập nhật sản phẩm thành công!!!")
        Me.Close()
    End Sub


    Private Sub frmThem_CapNhatKho_FormClosed(sender As System.Object, e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        frmXemKho.LayHangHoa()
    End Sub

    Private Sub btnThoat_Click(sender As System.Object, e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub btnHinhAnh_Click(sender As System.Object, e As System.EventArgs) Handles btnHinhAnh.Click
        Dim op As New System.Windows.Forms.OpenFileDialog
        op.InitialDirectory = Application.StartupPath
        op.Filter = "Bitmap Files (*.bmp;*.dib)|*.bmp;*.dib|JPEG (*.jpg;*.jpeg;*.jpe;*.jfif)|*.jpg;*.jpeg;*.jpe;*.jfif|Gif (*.gif)|*.gif|TIFF (*.tif;*.tiff)|*.tif;*.tiff|PNG (*.png)|*.png|ICO (*.ico)|*.ico|All Picture Files|*.bmp;*.dib;*.jpg;*.jpeg;*.jpe;*.jfif;*.gif;*.tif;*.tiff;*.png;*.ico |All files (*.*)|*.*"
        If op.ShowDialog() = DialogResult.OK Then
            Dim filePath As String = op.FileName
            picHinhAnh.ImageLocation = Path.GetFullPath(filePath)
        End If
    End Sub

    Private Sub cbbDonVi_DropDown(sender As System.Object, e As System.EventArgs) Handles cbbDonVi.DropDown
        dt = TapHoaDAO.LayTatCaDonVi()
        cbbDonVi.DataSource = dt
        cbbDonVi.DisplayMember = "dvt_Loai"
        cbbDonVi.ValueMember = "dvt_ID"
    End Sub

    Private Sub cbbNhaCungCap_DropDown(sender As System.Object, e As System.EventArgs) Handles cbbNhaCungCap.DropDown
        dt = TapHoaDAO.LayTatCaNhaCungCap()
        cbbNhaCungCap.DataSource = dt
        cbbNhaCungCap.DisplayMember = "ncc_Ten"
        cbbNhaCungCap.ValueMember = "ncc_ID"
    End Sub

    Private Sub cbbXuatXu_DropDown(sender As System.Object, e As System.EventArgs) Handles cbbXuatXu.DropDown
        dt = TapHoaDAO.LayTatCaXuatXu()
        cbbXuatXu.DataSource = dt
        cbbXuatXu.DisplayMember = "xx_NoiXuatXu"
        cbbXuatXu.ValueMember = "xx_ID"
    End Sub

    Private Sub cbbLoaiHang_DropDown(sender As System.Object, e As System.EventArgs) Handles cbbLoaiHang.DropDown
        dt = TapHoaDAO.LayTatCaLoaiHang()
        cbbLoaiHang.DataSource = dt
        cbbLoaiHang.DisplayMember = "lh_Loai"
        cbbLoaiHang.ValueMember = "lh_ID"
    End Sub

    Private Sub cbbViTri_DropDown(sender As System.Object, e As System.EventArgs) Handles cbbViTri.DropDown
        dt = TapHoaDAO.LayTatCaViTri()
        cbbViTri.DataSource = dt
        cbbViTri.DisplayMember = "vt_NoiVT"
        cbbViTri.ValueMember = "vt_ID"
    End Sub
End Class